package com.yy.okhttp.chain;

import com.yy.okhttp.HttpCodec;
import com.yy.okhttp.Interceptor;
import com.yy.okhttp.InterceptorChain;
import com.yy.okhttp.Request;
import com.yy.okhttp.Response;

import java.io.IOException;
import java.util.Map;

/**
 * User: gongyongfeng2@yy.com
 * Date: 2020/8/11 22 :00
 */
public class HeaderInterceptor implements Interceptor {
    @Override
    public Response intercept(InterceptorChain chain) throws IOException {
        Request request = chain.call().request();
        Map<String, String> headers = request.getHeaders();
        headers.put(HttpCodec.HEAD_HOST, request.getUrl().getHost());
        headers.put(HttpCodec.HEAD_CONNECTION, HttpCodec.HEAD_VALUE_KEEP_ALIVE);
        if (request.getBody() != null) {
            String contentType = request.getBody().contentType();
            if (contentType != null) {
                headers.put(HttpCodec.HEAD_CONTENT_TYPE, contentType);
            }

            long contentLength = request.getBody().contentLength();
            if (contentLength != -1) {
                headers.put(HttpCodec.HEAD_CONTENT_LENGTH, Long.toString(contentLength));
            }
        }
        return chain.proceed();
    }
}
